<?php

/*
 *  Esta clase extiende el adapter de oracle para soportar Zend_Date
 *  al insertar y hacer update.
 */
class Ext_Db_Adapter_Mysqli extends Zend_Db_Adapter_Mysqli
{

    protected $_defaultStmtClass = 'Ext_Db_Statement_Mysqli';

    public function getDateDbExpr( $dateObj = null ) {
        if ( $dateObj === null ||
            $dateObj === Ext_Db_Date::NOW ) {
            return new Zend_Db_Expr( " now() " );
        }
        if ( $dateObj instanceof Ext_Db_Date ) {
            if ( $dateObj->isNow() ) {
                return new Zend_Db_Expr( " now() " );
            }
        } else {
            if ( ! $dateObj instanceof Zend_Date ) {
                throw new Zend_Db_Exception( "Invalid date" );
            }
        }
        return new Zend_Db_Expr( $dateObj->get( Zend_Date::YEAR ) .
                                '-' . $dateObj->get( Zend_Date::MONTH ) .
                                "-" . $dateObj->get( Zend_Date::DAY ) .
                                " " . $dateObj->get( Zend_Date::HOUR ) .
                                ":" . $dateObj->get( Zend_Date::MINUTE )  .
                                ":" . $dateObj->get( Zend_Date::SECOND ) );
    }

    public function getCurrDate() {
        return $this->fetchOne( "select now()" );
    }



    /**
     * Safely quotes a value for an SQL statement.
     *
     * If an array is passed as the value, the array values are quoted
     * and then returned as a comma-separated string.
     *
     * @param mixed $value The value to quote.
     * @param mixed $type  OPTIONAL the SQL datatype name, or constant, or null.
     * @return mixed An SQL-safe quoted value (or string of separated values).
     */
    public function quote($value, $type = null)
    {
        if ( $value instanceof Ext_Db_Date ) {
            $value = $this->getDateDbExpr( $value );
        }
        return parent::quote($value, $type);
    }

    protected function processBindVars( $bind ) {
        foreach ( $bind as $col => $val ) {
            if ( $val instanceof Zend_Date || $val instanceof Ext_Db_Date ) {
                $bind[ $col ] = $this->getDateDbExpr( $val );
            }
        }
        return $bind;
    }

    /**
     * Prepares and executes an SQL statement with bound data.
     *
     * @param  mixed  $sql  The SQL statement with placeholders.
     *                      May be a string or Zend_Db_Select.
     * @param  mixed  $bind An array of data to bind to the placeholders.
     * @return Zend_Db_Statement_Interface
     */
    public function query( $sql, $bind = array() ) {
        return parent::query( $sql,  $this->processBindVars( $bind ) );
    }

    // Se reemplazan los Ext_Db_Date o Zend_Date por las expresiones que corresponden.
    public function insert( $table, array $bind ) {
        return parent::insert( $table, $this->processBindVars( $bind ) );
    }

    public function update( $table, array $bind, $where = '' ) {
        return parent::update( $table, $this->processBindVars( $bind ), $where );
    }

    public function foldArrayKeys( $data ) {
        $rows = array();
        if ( empty( $data ) ) {
            return $data;
        }
        foreach( $data as $key=>$value ) {
            if ( is_numeric( $key ) ) {
                $rows[ $key ] = $value;
            } else {
                $rows[ $this->foldCase( $key ) ] = $value;
            }
        }
        return $rows;
    }

}
